﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Data.SqlClient;
using System.Data;
using Restaurant.DTO;

namespace Restaurant.DAO
{
    public class XuatKhoDB
    {
        private SqlConnection conn = KetNoiDB.TaoKetNoi();

        public DataTable LayXuatKho()
        {
            try
            {
                
                conn.Open();
                string strCmd = "dbo.spLayXuatKho";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public DataTable LayXuatKhoTheoMa(int maXuatKho)
        {
            try
            {
                
                conn.Open();
                string strCmd = "dbo.spLayXuatKhoTheoMa";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNhapHang", maXuatKho));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                throw;
            }
        }

        public DataTable LayNLXuatKho(int maKho)
        {
            try
            {

                conn.Open();
                string strCmd = "dbo.spLayNLXuatKho";
                SqlCommand cmd = new SqlCommand(strCmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", maKho));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtab = new DataTable();
                adapt.Fill(dbtab);
                conn.Close();
                return dbtab;
            }
            catch (Exception ex)
            {
                
                throw;
            }
        }

        public bool ThemXuatKho(ref XuatKho xk)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spThemXuatKho";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ngayXuat", xk.NGAYXUAT));
                cmd.Parameters.Add(new SqlParameter("@maKho", xk.MAKHO));
                cmd.Parameters.Add(new SqlParameter("@maXuatKho", xk.MAXUATKHO));
                cmd.Parameters["@maXuatKho"].Direction = ParameterDirection.Output;
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                xk.MAXUATKHO = int.Parse(cmd.Parameters["@maXuatKho"].Value.ToString());
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool ThemCTXuatKho(int maXuatKho, int maNL, float soLuong, int maKho)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spThemCTXuatKho";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maXuatKho", maXuatKho));
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                cmd.Parameters.Add(new SqlParameter("@soLuong", soLuong));
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                conn.Close();
                return false;
            }
        }

        public bool SuaXuatKho(XuatKho xk)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spSuaXuatKho";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maXuatKho", xk.MAXUATKHO));
                cmd.Parameters.Add(new SqlParameter("@ngayXuat", xk.NGAYXUAT));
                cmd.Parameters.Add(new SqlParameter("@maKho", xk.MAKHO));
                cmd.Parameters.Add(new SqlParameter("@dbCTNhap", xk.CHITIETXUAT));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaXuatKho(int ma)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spXoaXuatKho";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maXuatKho", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaCTXuatKho(int maXuatKho)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spXoaCTXuatKho";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maXuatKho", maXuatKho));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                conn.Close();
                return false;
            }
        }

        public bool ThemCTXuatKhoDeadlockError(int maXuatKho, int maNL, float soLuong, int maKho)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spDeadlock_ThemCTXuatKho_Error";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maXuatKho", maXuatKho));
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                cmd.Parameters.Add(new SqlParameter("@soLuong", soLuong));
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                conn.Close();
                return false;
            }
        }

        public bool ThemCTXuatKhoDeadlock(int maXuatKho, int maNL, float soLuong, int maKho)
        {
            try
            {
                conn.Open();
                string strcmd = "dbo.spDeadlock_ThemCTXuatKho_Fixed";
                SqlCommand cmd = new SqlCommand(strcmd, conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maXuatKho", maXuatKho));
                cmd.Parameters.Add(new SqlParameter("@maNL", maNL));
                cmd.Parameters.Add(new SqlParameter("@soLuong", soLuong));
                cmd.Parameters.Add(new SqlParameter("@maKho", maKho));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                conn.Close();
                return true;
            }
            catch (Exception ex)
            {
                conn.Close();
                return false;
            }
        }
    }
}
